<?php
defined('IN_MAIN') or exit('Invalid Access!');

function format_prod_display_invoice($prods, $delivery_cost){
/*
This function format the $prods into bordered table with info refno, title (sub-product if jumpstart), pv, qty, total pv, grand total pv
input $prods must have the format
$prods[index][refno]
$prods[index][prod_type]
$prods[index][title]
$prods[index][pv]
$prods[index][qty]
*/

	global $vars;

	for($i=0,$t=count($prods);$i<$t;$i++){
		if($prods[$i]["prod_type"]=="p"){
			$pck_prods='';
			for($j=0,$t2=count($prods[$i]["pck_prod"]);$j<$t2;$j++){
				$pck_prods.="<div style='margin-left:5px;'>".$prods[$i]["pck_prod"][$j]["refno"].", ".$prods[$i]["pck_prod"][$j]["title"].", ".__("Qty").__(":")." ".$prods[$i]["pck_prod"][$j]["qty"]."</div>";
			}
		}
		$prod_row.="
		<tr>
			<td>".$prods[$i]["refno"]."</td>
			<td>".$prods[$i]["title"].($prods[$i]["prod_type"]=="p"? "<br />\n$pck_prods" : "")."</td>
			<td align='right'>".number_format($prods[$i]["bv"], 2)."</td>
			<td align='right'>".number_format($prods[$i]["dp"], 2)."</td>
			<td align='right'>".number_format($prods[$i]["rp"], 2)."</td>
			<td align='center'>".$prods[$i]["qty"]."</td>
			<td align='right'>".number_format($prods[$i]["bv"] * $prods[$i]["qty"], 2)."</td>
			<td align='right'>".number_format($prods[$i]["dp"] * $prods[$i]["qty"], 2)."</td>
			<td align='right'>".number_format($prods[$i]["rp"] * $prods[$i]["qty"], 2)."</td>
		</tr>";
		$grand_bv += $prods[$i]["bv"] * $prods[$i]["qty"];
		$grand_dp += $prods[$i]["dp"] * $prods[$i]["qty"];
		$grand_rp += $prods[$i]["rp"] * $prods[$i]["qty"];
	}
	$formatted_prod=
	"<table class='product-detail'>
		<tr>
			<th width='65'>".__("Item Code")."</th>
			<th>".__("Title")."</th>
			<th width='50'>BV</th>
			<th width='50'>DP</th>
			<th width='50'>CP</th>
			<th width='30'>".__("Qty")."</th>
			<th width='70'>".replace_tag(__("Total <%title%>"), array("<%title%>"=>__($vars["bv_title"])))."</th>
			<th width='70'>".replace_tag(__("Total <%title%>"), array("<%title%>"=>__("DP")))."</th>
			<th width='70'>".replace_tag(__("Total <%title%>"), array("<%title%>"=>__("CP")))."</th>
		</tr>
		$prod_row
		<tr>
			<th colspan='6' align='right'>".__("Delivery Cost")."</th>
			<th align='right'>".number_format(0, 2)."</th>
			<th align='right'>".number_format($delivery_cost, 2)."</th>
			<th align='right'>".number_format($delivery_cost, 2)."</th>
		</tr>
		<tr>
			<th colspan='6' align='right'>".__("Order Total")."</th>
			<th align='right'>".number_format($grand_bv, 2)."</th>
			<th align='right'>".number_format($grand_dp + $delivery_cost, 2)."</th>
			<th align='right'>".number_format($grand_rp + $delivery_cost, 2)."</th>
		</tr>
	</table>";

	return $formatted_prod;
}
?>